package com.pace.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.pace.service.impl.DemoImg;
import com.pace.vo.ExcelInfo;
import com.pace.vo.OrderExcel;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.ocr.v20181119.OcrClient;
import com.tencentcloudapi.ocr.v20181119.models.RecognizeTableOCRRequest;
import com.tencentcloudapi.ocr.v20181119.models.RecognizeTableOCRResponse;

import java.io.File;
import java.util.List;

public class RecognizeTableOCR {

    public static void main(String[] args) {
        String path = "D:\\pri-code\\paceToPace\\pace-tools\\src\\main\\resources\\img\\微信图片_20201126161811.jpg";
        String newFileName = "D:\\pri-code\\paceToPace\\pace-tools\\src\\main\\resources\\transfer4X3\\" + "export" + "_" + System.currentTimeMillis() + ".xlsx";
        String imgStr = getImgStr(path) + "第页";
        DemoImg demoImg = new DemoImg();
        String[] arrLine = imgStr.split("\\s+");
        List<OrderExcel> orderExcels = demoImg.toX3Order(arrLine);
        File newExcel = new File(newFileName);
        File template = new File("D:\\pri-code\\paceToPace\\pace-tools\\src\\main\\resources\\template\\sl_order_template.xlsx");
        Excel.toExcel(orderExcels, newExcel, template);

    }


    public static String getImgStr(String path) {
        String rtStr = "";
        try {

            Credential cred = new Credential("AKIDwd9CShES4WicNEIOZaPxNRWTGAyVljjW ", "gHbfkaAuVthaMtBE0c69vSjkDUh8Zlk6");

            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint("ocr.tencentcloudapi.com");

            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);

            OcrClient client = new OcrClient(cred, "ap-shanghai", clientProfile);
            RecognizeTableOCRRequest req = new RecognizeTableOCRRequest();

            String imgFileToBase64 = BaseImg64.getImgFileToBase64(path);
            req.setImageBase64(imgFileToBase64);

            RecognizeTableOCRResponse resp = client.RecognizeTableOCR(req);
            String toJsonString = RecognizeTableOCRResponse.toJsonString(resp);
            JSONObject jsonObject = JSON.parseObject(toJsonString);
            List<String> jsonObject2 = JSON.parseArray(jsonObject.get("TableDetections").toString(), String.class);
            for (String jsonObject21 : jsonObject2) {
                JSONObject jsonObject32 = JSON.parseObject(jsonObject21);
                List<String> jsonObject23 = JSON.parseArray(jsonObject32.get("Cells").toString(), String.class);
                for (String jsonObject231 : jsonObject23) {
                    ExcelInfo excelInfo = JSON.parseObject(jsonObject231, ExcelInfo.class);
                    rtStr += excelInfo.getText() + " ";
                }
            }
        } catch (TencentCloudSDKException e) {
            new RRException("解析错e={}", e);
        }
        return rtStr;
    }


}